package com.yueya.api.nursing;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.yueya.business.nursing.model.NursingBase;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;

import net.sf.json.JSONObject;

public class INursingService extends BaseService {
	
	
	public Resp save (JSONObject jo) {
		return jo.containsKey("nursingbaseid")?update(jo):add(jo);
	}
	
	public Resp add(JSONObject jo) {
		String seniorid = jo.getString("seniorid");
		String nursingstandardsid = jo.getString("nursingstandardsid");
		String empid = jo.getString("empid");
		
		NursingBase nursingbase = NursingBase.dao.findById( jo.getString("nursingbaseid"));
		nursingbase.set("nursing_standards_id", nursingstandardsid);
		nursingbase.set("emp_id", empid);
		nursingbase.set("senior_id", seniorid);
		// 咨询内容
		if (jo.containsKey("remark")) {
			String remark = jo.getString("remark");
			nursingbase.set("remark", remark);
		}
		// 咨询方式
		if (jo.containsKey("nursing")) {
			String reservtype = jo.getString("nursing");
			nursingbase.set("nursing", reservtype);
		}
		boolean bl = NursingBase.dao.save(nursingbase);
		if (bl) {
			resp.setCode(CodeConst.SUCCESS);
			resp.setMsg("新增成功");
		} else {
			resp.setCode("30144");
			resp.setMsg("新增失败");
		}
		return resp;
	}
    
	/**
	 * 
	 */
	public Resp update(JSONObject jo) {

		if (containsKey(jo, "seniorid", "nursingstandardsid","empid")) {
			
			String seniorid = jo.getString("seniorid");
			String nursingstandardsid = jo.getString("nursingstandardsid");
			String empid = jo.getString("empid");
			
			NursingBase nursingbase = NursingBase.dao.findById( jo.getString("nursingbaseid"));
			nursingbase.set("nursing_standards_id", nursingstandardsid);
			nursingbase.set("emp_id", empid);
			nursingbase.set("senior_id", seniorid);
			// 咨询内容
			if (jo.containsKey("remark")) {
				String remark = jo.getString("remark");
				nursingbase.set("remark", remark);
			}
			// 咨询方式
			if (jo.containsKey("nursing")) {
				String reservtype = jo.getString("nursing");
				nursingbase.set("nursing", reservtype);
			}
			boolean bl = NursingBase.dao.update(nursingbase);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("修改成功");
			} else {
				resp.setCode("30144");
				resp.setMsg("修改失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	
	//获取列表
	@SuppressWarnings("rawtypes")
	public Resp getPageList(JSONObject jo) {
		
		if (containsKey(jo, "pgn", "pgs")) {
			pageNumber = jo.getInt("pgn");
			pageSize = jo.getInt("pgs");
			Map<String, String> param = new HashMap<String, String>();

			// 老人姓名
			if (jo.containsKey("seniorname")) {
				String seniorname = jo.getString("seniorname");
				param.put("senior_name", seniorname);
			}

			// 房间号
			if (jo.containsKey("bedno")) {
				String bedno = jo.getString("bedno");
				param.put("bedno", bedno);
			}
			
			// 护理级别
			if (jo.containsKey("bedno")) {
				String bedno = jo.getString("bedno");
				param.put("bedno", bedno);
			}

			String select = "select sb.* ";
			String sqlExceptSelect=" from  v_senior_base sb ";
			Page page = Db.paginate(pageNumber, pageSize, select, sqlExceptSelect);
			if(page!=null) {
				resp.setCode(CodeConst.SUCCESS);
				if (page.getList().size() > 0) {
					resp.setData(page);
				}
			}else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no record");
			}
					
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}
	
	

}
